import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import {getMenus, getToken, removeAllCache} from "@/utils/storage";
import {UserModule} from "@/store/modules/user";
import {Route} from "vue-router";

import {initWebSocket} from "@/utils/webSocket";
import {errorMsg} from "@/utils/message";
import router, {resetRouter} from "@/router/router";
import {refreshDict} from "@/utils/kit";

NProgress.configure({showSpinner: false});

router.beforeEach(async (to: Route, _: Route, next: any) => {
        if (1 == 1) {
            next();
            return;
        }
        //判断是否刷新界面
        if (UserModule.refreshStatus.length == 0) {
            await refreshDict();
        }

        // //////console.log("fullPath", to.fullPath);
        NProgress.start();
        if (getToken()) {
            //微信端不具备路由，这里直接通过，登录即可无需其他检查
            if (to.path.indexOf("wx/") != -1) {
                next();
                return;
            }
            // //////console.log(16);
            //////console.log(19);
            //不具备菜单
            if (getMenus().length == 0) {
                //////console.log(22);
                removeAllCache();
                next(`/login`);
                errorMsg("当前账户不具备访问此路径权限.");
                return;
            }
            //根路径
            if (to.path === '' || to.path == "/login") {
                // //////console.log(30);
                next({path: '/'});
                NProgress.done();
                return;
            }

            //判断是否刷新界面
            if (UserModule.refreshStatus.length != 0) {
                //console.log(38);
                next()
            } else {
                //console.log(41);
                initWebSocket();
                await UserModule.setStatus();
                await refreshDict();
                next({...to, replace: false})
            }
        } else {
            // http://10.0.1.10:8002/wx/selectZone
            resetRouter();
            //console.log(36);
            if (to.path.indexOf("/app/redirectLogin") != -1) {
                next();
                return;
                //     if (to.path == "/wx/welcome" || to.path == "/wx/login" || to.path == "/wx/password") {
                //     } else {
                //         next("/wx/welcome");
                //     }
                //     return;
            }

            if (to.path == "/login") {
                // //////console.log(42);
                next();
            } else {
                // //////console.log(46);
                next(`/login?redirect=${to.path}`);
                NProgress.done()
            }
        }
    }
);

/**
 * 路由跳转后--监听
 */
router.afterEach((to: Route) => {
    // if (to.path.indexOf("wx/") != -1) {
    //     document.title = "战“疫”应用超市后台管理系统";
    // } else {
    document.title = to.meta.title;
    // }
    NProgress.done();
});
export default router
